<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <Checkbox v-model:checked="dataRef.isUseMsg">短消息通知</Checkbox>
        <Checkbox v-model:checked="dataRef.isToMobile" v-if="serverInfo.isUseSms"
          >短信通知</Checkbox
        >
        <a-button type="primary" @click="handleReset"> 重置全部代理 </a-button>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              title: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <SetupProxyDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    getUserProxyList,
    getUserProxyDel,
    getUserProxyReset,
  } from '/@/api/flowManage/flowManage';
  import { useDrawer } from '/@/components/Drawer';
  import SetupProxyDrawer from './setupProxyDrawer.vue';
  import { columns, searchFormSchema } from './setupProxy.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Checkbox } from 'ant-design-vue';
  import { useUserStore } from '/@/store/modules/user';
  export default defineComponent({
    name: 'SetupProxy',
    components: { BasicTable, TableAction, SetupProxyDrawer, Checkbox },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { createMessage, createConfirm } = useMessage();
      const userStore = useUserStore();
      const serverInfo = userStore.getServerInfo;
      const dataRef = ref({
        isUseMsg: true,
        isToMobile: true,
      });
      onMounted(async () => {});

      const [registerTable, { reload }] = useTable({
        title: '',
        api: getUserProxyList,
        columns,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await getUserProxyDel({ id: record.id }).then(() => {
          handleSuccess();
        });
      }

      const handleReset = () => {
        createConfirm({
          iconType: 'warning',
          title: '您确定要重置吗？',
          onOk: async () => {
            const data: Recordable = {
              isUseMsg: dataRef.value.isUseMsg,
            };
            if (serverInfo.isUseSms) {
              data.isToMobile = dataRef.value.isToMobile;
            }
            await getUserProxyReset(data).then(() => {
              handleSuccess();
              createMessage.success('操作成功');
            });
          },
        });
      };

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleReset,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        dataRef,
        serverInfo,
      };
    },
  });
</script>
